package com.yige.web.controller

import com.mongodb.BasicDBObject
import com.mongodb.DBObject
import com.yige.common.ArgsUtil
import com.yige.common.MD5Util
import com.yige.common.ResultUtil
import com.yige.common.SystemConstant
import com.yige.common.ToDBObjectUtil
import com.yige.service.mongoService.MongoManagerService
import com.yige.service.mongoService.MongoUserService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ResponseBody

/**
 * Created by sunhao on 2015/11/5 0005.
 */

@Controller
@RequestMapping("manager")
class ManagerController {

    @Autowired
    MongoManagerService managerService
    @Autowired
    MongoUserService userService

    @RequestMapping("list.do")
    def list(Model model){
        def list = managerService.findList()
        model.addAttribute("managers",list)
        return "manager/list"
    }

    @RequestMapping("addManager")
    @ResponseBody
    def addManager(String name,String phone){
        def manager = [_id:UUID.randomUUID().toString(),name:name,phone:phone,registerTime:new Date()]as BasicDBObject
        addToUser(name,phone,manager.get("_id"))
        boolean flag = managerService.insertOne(manager)
        return ResultUtil.recallAjax(flag)
    }

    @RequestMapping("delManager")
    @ResponseBody
    def delManager(@RequestParam("_ids") List<String> _ids){
        boolean flag = false
        def user
        _ids.each {_id->
            flag = managerService.deleteOne(_id)
            user = userService.findOneByQuery([identity:_id]as BasicDBObject)
            userService.deleteOne(user._id)
        }
        return ResultUtil.recallAjax(flag)
    }

    @RequestMapping("updateManager")
    @ResponseBody
    def updateManager(String _id, String name,String phone){
        def manager_db = managerService.findOneById(_id)
        def manager = [_id:_id,name:name,phone:phone,registerTime:manager_db.registerTime]as BasicDBObject
        boolean flag = managerService.updateOne(_id,manager)
        return ResultUtil.recallAjax(flag)
    }

    def addToUser(String name,String phone,String identity){
        List<DBObject> role = new LinkedList<>()
        Map<String,Object> userInfo = [_id:UUID.randomUUID().toString(),
                                       username:name,
                                       password:SystemConstant.DEFAULT_PASSWORD,
                                       email:phone,
                                       status:SystemConstant.USER_ON_JOB,
                                       role:role,
                                       identity:identity,
                                       degree:"manager"
        ]
        DBObject dbObject = ToDBObjectUtil.toDBObject(userInfo)
        saveUser(dbObject)
    }

    def saveUser(DBObject userInfo){
        ArgsUtil.checkArgs(userInfo)
        // 判断用户名是否存在
        DBObject user_db = userService.findUserByName(userInfo.username)
        if (user_db == null) {
            // 用户名不存在，可以创建用户
            // 新增用户
            String md5Password = MD5Util.md5(userInfo.password)
            userInfo.put("password",md5Password)
            userInfo.put("registerTime",new Date(System.currentTimeMillis()))
            userService.insertOne(userInfo)
            return true
        } else {
            // 用户名已存在
            return false
        }
    }

}
